using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Entities;

namespace DAL
{
   public class tbKhachhangDAL
    {
        private SqlConnection cn = null;
        public tbKhachhangDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
        public void Insert(tbKhachhang kh)
        {
            SqlCommand cm = new SqlCommand("Khach_hang_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@tenkh", kh.Tenkh);
            cm.Parameters.AddWithValue("@taikhoan", kh.Taikhoan);
            cm.Parameters.AddWithValue("@matkhau", kh.Matkhau);
            cm.Parameters.AddWithValue("@sodt", kh.Sodthoai);
            cm.Parameters.AddWithValue("@email", kh.Email);
            cm.Parameters.AddWithValue("@diachi", kh.Diachi);
            cm.ExecuteNonQuery();
        }
        public void Update(tbKhachhang kh)
        {
            SqlCommand cm = new SqlCommand("Khach_hang_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@makh", kh.Makh);
            cm.Parameters.AddWithValue("@tenkh", kh.Tenkh);
            cm.Parameters.AddWithValue("@taikhoan", kh.Taikhoan);
            cm.Parameters.AddWithValue("@matkhau", kh.Matkhau);
            cm.Parameters.AddWithValue("@sodt", kh.Sodthoai);
            cm.Parameters.AddWithValue("@email", kh.Email);
            cm.Parameters.AddWithValue("@diachi", kh.Diachi);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("Khach_hang_Delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@makh",iD);
            cm.ExecuteNonQuery();
        }
        public tbKhachangCollection Get(string where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("Khach_hang_Get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbKhachangCollection tbkhachhangCollection = new tbKhachangCollection();

            while (dr.Read())
            {
                tbKhachhang tbkhachhang = new tbKhachhang();
                tbkhachhang.Makh = int.Parse(dr.GetValue(0).ToString());
                tbkhachhang.Tenkh = dr.GetValue(1).ToString();
                tbkhachhang.Taikhoan = dr.GetValue(2).ToString();
                tbkhachhang.Matkhau = dr.GetValue(3).ToString();
                tbkhachhang.Sodthoai = dr.GetValue(4).ToString();
                tbkhachhang.Email = dr.GetValue(5).ToString();
                tbkhachhang.Diachi = dr.GetValue(6).ToString();
                tbkhachhangCollection.Add(tbkhachhang);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return tbkhachhangCollection;
        
    }
       public tbKhachhang GetByEmail(String em)
       {
           string cautruyvan = "select * from Khach_hang WHERE Email='" + em + "'";
           SqlDataAdapter da = new SqlDataAdapter(cautruyvan, cn);
           DataTable tb = new DataTable("Khach_hang");
           try
           {
               da.Fill(tb);
           }
           catch
           {
               return null;
           }
           tbKhachhang kh = new tbKhachhang();
           if (tb.Rows.Count > 0)
           {
               DataRow dr = tb.Rows[0];
               kh.Makh = int.Parse(dr["Makh"].ToString());
               kh.Tenkh = dr["Tenkh"].ToString();
               kh.Taikhoan = dr["Taikhoan"].ToString();
               kh.Matkhau = dr["Matkhau"].ToString();
               kh.Sodthoai = dr["Sodt"].ToString();
               kh.Email = dr["Email"].ToString();
               kh.Diachi = dr["Diachi"].ToString();
               return kh;
           }
           return null;
       }
       //public tbKhachhang GetByTendangnhap(string tdn)
       //{
       //    string cautruyvan = "select * from Khach_hang WHERE taikhoan='" + tdn + "'";
       //    SqlDataAdapter da = new SqlDataAdapter(cautruyvan, cn);
       //    DataTable tb = new DataTable("Khach_hang");
       //    try
       //    {
       //        da.Fill(tb);
       //    }
       //    catch
       //    {
       //        return null;
       //    }
       //    tbKhachhang kh = new tbKhachhang();
       //    if (tb.Rows.Count > 0)
       //    {
       //        DataRow dr = tb.Rows[0];
       //        kh.Makh = int.Parse(dr["Makh"].ToString());
       //        kh.Tenkh = dr["Tenkh"].ToString();
       //        kh.Taikhoan = dr["Taikhoan"].ToString();
       //        kh.Matkhau = dr["Matkhau"].ToString();
       //        kh.Sodthoai = dr["Sodt"].ToString();
       //        kh.Email = dr["Email"].ToString();
       //        kh.Diachi = dr["Diachi"].ToString();
       //        return kh;
       //    }
       //    return null;
       //}
}
}
